package com.google.code.training_java_basic.oop;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

import org.apache.log4j.Logger;

public class AlarmParser {

	private static final int NUMBER_OF_COLUMNS_IN_FILE = 6;
	private static final Logger logger = Logger.getLogger(AlarmParser.class);

	private Alarm createAlarm(String fileRow, String networkElement){
		
		Alarm result = new Alarm();
		String[] line = fileRow.split(";");
		int alarmId = Integer.parseInt(line[0]);
		int alarmNumber = Integer.parseInt(line[1]);
		int severity = Integer.parseInt(line[2]);
		String alarmText = (String) line[3];
		String additionalInfo = (String) (line[4]);
		Date clearTime = null;
		Date parsedAlarmTime = null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
		
		try {
			parsedAlarmTime = dateFormat.parse(line[5]);
		} catch (ParseException e) {
			logger.error(LoggerFormatter.format(1,LoggerFormatter.Level.ERROR,e.getStackTrace().toString()));
		}
		Date alarmTime = new Date(parsedAlarmTime.getTime());
		Date parsedClearTime = null;

		if (line.length > NUMBER_OF_COLUMNS_IN_FILE) {
			try {
				parsedClearTime = dateFormat.parse(line[6]);
				clearTime = new Date(parsedClearTime.getTime());
				result.setCancelTime(clearTime);
			} catch (ParseException e) {
				logger.error(LoggerFormatter.format(1,LoggerFormatter.Level.ERROR,e.getStackTrace().toString()));
			}
		}
		result.setAlarmId(alarmId);
		result.setAlarmNumber(alarmNumber);
		result.setSeverity(severity);
		result.setAlarmText(alarmText);
		result.setAdditionalInfo(additionalInfo);
		result.setAlarmTime(alarmTime);
		result.setSourceNe(networkElement);
		
		return result;		
	}
	
	public List<Alarm> retrieveAlarmsFromFile(String fileName, String networkElement) {
		List<Alarm> result = new LinkedList<Alarm>();
		FileReader alarmfileReader = null;
		BufferedReader alarmBuffReader = null;
		try {
			alarmfileReader = new FileReader(new File(fileName));
			alarmBuffReader = new BufferedReader(alarmfileReader);
			String fileRow = "";
			Scanner inputScanner = new Scanner(alarmBuffReader);
	 		while (inputScanner.hasNextLine()) {
				fileRow = inputScanner.nextLine();
				result.add(createAlarm(fileRow,networkElement));
			}
		} catch (FileNotFoundException e) {
			logger.error(LoggerFormatter.format(1,LoggerFormatter.Level.ERROR,e.getStackTrace().toString()));
		}
		finally {
			if(alarmBuffReader != null){
				try {
					alarmBuffReader.close();
				}
			 	catch (IOException ex) {
					logger.error(LoggerFormatter.format(1,LoggerFormatter.Level.ERROR,ex.getStackTrace().toString()));
			 	}
			}
			if(alarmfileReader != null){
				try {
					alarmfileReader.close();
				}
			 	catch (IOException ex) {
					logger.error(LoggerFormatter.format(1,LoggerFormatter.Level.ERROR,ex.getStackTrace().toString()));
			 	}
			}
		}
		return result;
	}

}


